package com.xinhua.erp.service;

import com.xinhua.erp.pojo.PageResult;
import com.xinhua.erp.pojo.Product;
import com.xinhua.erp.pojo.Result;
import com.xinhua.erp.repo.ProductRepo;
import com.xinhua.erp.utils.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
@Transactional

public class ProductService {

    @Autowired
    private ProductRepo productRepo;

    @Autowired
    private IdWorker idWorker;


    public Result findById(long id){

        return Result.OK(productRepo.getOne(id));

    }

    public Result findAll(int page,int size){

        Pageable pageable= PageRequest.of(page - 1, size);
        Page<Product> products = productRepo.findAll(pageable);

        return Result.OK(new PageResult<>(products.getTotalPages(),products.getContent()));

    }

    public Result findAllProduct(){
        List<Product> list = productRepo.findAll();
        return Result.OK(list);
    }

    public Result deleteById(long id){

        productRepo.deleteById(id);

        return Result.OK();

    }

    public Result save(Product product){

        product.setId(idWorker.nextId());

        productRepo.save(product);

        return Result.OK();

    }

    public Result update(Product product,long id){

        product.setId(id);

        productRepo.save(product);

        return Result.OK();

    }

    public Result Search(Map searchMap, int page, int size){
        Pageable pageable=PageRequest.of(page-1, size);
        Page<Product> products= productRepo.findAll(createSpecification(searchMap),pageable);
        return Result.OK(new PageResult<>(products.getTotalElements(),products.getContent()));
    }

    private Specification<Product> createSpecification(Map searchMap){
        return new Specification<Product>() {
            @Override
            public Predicate toPredicate(Root<Product> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                ArrayList<Predicate> predicateList=new ArrayList<>();
                if(!StringUtils.isBlank((String)searchMap.get("gategoryId"))){

                    predicateList.add(criteriaBuilder.like(root.get("gategoryId").as(String.class),"%"+searchMap.get("gategoryId")+"%"));

                }
                if(!StringUtils.isBlank((String)searchMap.get("name"))){

                    predicateList.add(criteriaBuilder.like(root.get("name").as(String.class),"%"+searchMap.get("name")+"%"));

                }
                if(!StringUtils.isBlank((String)searchMap.get("model"))){

                    predicateList.add(criteriaBuilder.like(root.get("model").as(String.class),"%"+searchMap.get("model")+"%"));

                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }


}
